<?php

/**
 * 老师 API
 * User: don
 */

namespace app\modules\app\controllers;

use yii;
use app\libs\AppControl;
use app\libs\Method;
use app\libs\Pager;
use app\libs\VerificationCode;
use app\libs\Sms;
use app\modules\content\models\Info;
use app\modules\content\models\Teachers;
use app\modules\content\models\VideoLog;

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

class TeacherController extends AppControl
{

    public $enableCsrfValidation = false;

    function init (){
        parent::init();
        include_once($_SERVER['DOCUMENT_ROOT'].'/../libs/ucenter/ucenter.php');
    }

    /**
     * [actionTeacherList 师资团队列表]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionList()
    {
        $page = Yii::$app->request->post('page', 1); // 页码
        $seniority = Yii::$app->request->post('seniority', 0); // 讲师级别 0全部 1首席 2总监 3金牌
        $subject = Yii::$app->request->post('subject', 0); // 主讲科目 0全部 听力 口语 阅读 写作
        $pageSize = 10;
        $data = Teachers::getTeacher($seniority, $subject, $page, $pageSize);
        
        if ($data) {
            foreach ($data as $key => $value) {
                $data[$key]['image'] = "https://www.thinkuprep.com".$value['pic'];
            }
        }

        die(json_encode(['code'=>1, 'data'=>$data]));
    }

    /**
     * [actionTeacherDetail 老师详情]
     * @Author don
     * @Date   2021-10-26
     * @return [type]     [description]
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Teachers::find()->asArray()->where('id='.$id)->one();
        $data['image'] = "https://www.thinkuprep.com".$data['pic'];
        // 视频播放次数+1
        Teachers::updateAll(['course_views'=>$data['course_views']+1], 'id=:id', array(':id' => $id));
        // 试听课相关信息
        $video = [];
        // 试听课sdk
        if ($data['url']) {
            $videoData = json_decode(Method::post('https://live.thinkwithu.com/api/play/pc-video', ['word'=>$data['url']]), true);
            if ($videoData['code'] == 1) {
                $video['course_video'] = $videoData['data']['video'];
            } else {
                $video['course_video'] = null;
            }
        } else {
            $video['course_video'] = null;
        }

        $video['course_title'] = !empty($data['course_title'])?$data['course_title']:null;
        $video['course_views'] = !empty($data['course_views'])?$data['course_views']:0;
        $video['course_tag'] = !empty($data['course_tag'])?explode(',', $data['course_tag']):null;
        $video['course_image'] = !empty($data['course_image'])?"https://www.thinkuprep.com".$data['course_image']:null;
        $video['course_description'] = !empty($data['course_description'])?"https://www.thinkuprep.com".$data['course_description']:null;
        
        die(json_encode(['code'=>1, 'data'=>$data, 'video'=>$video]));
    }

    /**
     * [actionVideoDetail 试听课详情]
     * @Author don
     * @Date   2021-11-09
     * @return [type]     [description]
     */
    public function actionVideoDetail()
    {
        $id = Yii::$app->request->post('id');
        $data = Teachers::find()->asArray()->where('id='.$id)->one();
        // 试听课相关信息
        $video = [];
        // 试听课sdk
        if ($data['url']) {
            $videoData = json_decode(Method::post('https://live.thinkwithu.com/api/play/pc-video', ['word'=>$data['url']]), true);
            if ($videoData['code'] == 1) {
                $video['course_video'] = $videoData['data']['video'];
            } else {
                $video['course_video'] = null;
            }
        } else {
            $video['course_video'] = null;
        }

        $video['course_title'] = !empty($data['course_title'])?$data['course_title']:null;
        $video['course_views'] = !empty($data['course_views'])?$data['course_views']:0;
        $video['course_tag'] = !empty($data['course_tag'])?explode(',', $data['course_tag']):null;
        $video['course_image'] = !empty($data['course_image'])?"https://www.thinkuprep.com".$data['course_image']:null;
        $video['course_description'] = !empty($data['course_description'])?"https://www.thinkuprep.com".$data['course_description']:null;

        // 查询播放记录
        $uid = Yii::$app->session->get('uid');
        $video['record'] = VideoLog::getTeacherVideoRecord($id, $uid);

        die(json_encode(['code'=>1, 'video'=>$video]));
    }
}
